/**
 * 网易云音乐API(搜索)
 */
var http = require('http');
var queryString = require('querystring');
var fs = require('fs');
//解决中文乱码的依赖
//var iconv = require('iconv-lite');
//var BufferHelper = require('bufferhelper');

var params = {
	s : '小小鸟',
	limit : 5,
	sub: false,
	type : 1,
	offset : 1
};

var postData = queryString.stringify(params);

var options = {  
        method: "POST",  
        host: "music.163.com",
        post : 80,
        path: "/api/search/get/",  
        headers: { 
        	"cookie":'appver=2.0.2',
        	"referer":'http://music.163.com',
            "Content-Type": 'application/x-www-form-urlencoded',  
            "Content-Length": postData.length  
        }  
    };  


//var buffer = new BufferHelper();

var req = http.request(options, function(res) {
	  console.log('状态码：');
//	  console.dir(res.statusCode,{colors:true});
	  console.dir(res.statusCode);
	  console.log('请求头：');
//	  console.dir(res.headers,{colors:true});
	  console.dir(res.headers);
	  var data = '';
	  res.setEncoding('utf-8');
	  res.on('data', function (chunk) {
//		  buffer.concat(chunk);
		  data += chunk;
	  }).on('end',function() {
//		  var str = iconv.decode(buffer.toBuffer(),'UTF8');
//		  console.log(str);
		  console.log('返回数据：');
		  console.dir(JSON.parse(data),{depth:10});
//		  console.dir(JSON.parse(data),{depth:10,colors:true});
	  });
	});

	req.on('error', function(e) {
	  console.log('problem with request: ' + e.message);
	});

	// write data to request body
	req.write(postData);
	req.end();